package cn.sinjet.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import cn.sinjet.entity.AppInfo;

public class AppInfoUtil {
    public static List<AppInfo> getAppList(Context context){
    	List<AppInfo> appList = new ArrayList<AppInfo>();
    	PackageManager pm = context.getPackageManager();
        // Return a List of all packages that are installed on the device.
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        for (PackageInfo packageInfo : packages) {
            // 判断系统/非系统应用
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) // 非系统应用
            {
            	Log.i("app", "3th party app pckName:"+packageInfo.packageName);
                AppInfo info = new AppInfo();
                info.appName = packageInfo.applicationInfo.loadLabel(pm).toString();
                info.packageName = packageInfo.packageName;
                info.appIcon = packageInfo.applicationInfo.loadIcon(pm);
                // 获取该应用安装包的Intent，用于启动该应用
                info.appIntent = pm.getLaunchIntentForPackage(packageInfo.packageName); 
                appList.add(info);
            } else {
                // 系统应用
            	Log.i("app", "system app pckName:"+packageInfo.packageName);
            	final String baiduMapName = "com.baidu.BaiduMap";
            	final String baiduNaviName = "com.baidu.navi";
            	final String weChatName = "com.tencent.mm";
            	if(packageInfo.packageName.equals(baiduMapName)
            			||packageInfo.packageName.equals(baiduNaviName)
            			||packageInfo.packageName.equals(weChatName)){
            		AppInfo info = new AppInfo();
                    info.appName = packageInfo.applicationInfo.loadLabel(pm).toString();
                    info.packageName = packageInfo.packageName;
                    info.appIcon = packageInfo.applicationInfo.loadIcon(pm);
                    // 获取该应用安装包的Intent，用于启动该应用
                    info.appIntent = pm.getLaunchIntentForPackage(packageInfo.packageName); 
                    appList.add(info);	    
            	}
            }
        }
        return appList;
    }
    public static void setAppInfo(AppInfo appInfo,Context context){
    	if(appInfo == null) return;
    	PackageManager pm = context.getPackageManager();
    	PackageInfo packageInfo;
		try {
			packageInfo = pm.getPackageInfo(appInfo.packageName, PackageManager.GET_ACTIVITIES);
			if(packageInfo != null){
	    		appInfo.appName = packageInfo.applicationInfo.loadLabel(pm).toString();
	    		appInfo.packageName = packageInfo.packageName;
	    		appInfo.appIcon = packageInfo.applicationInfo.loadIcon(pm);
	    		appInfo.appIntent = pm.getLaunchIntentForPackage(packageInfo.packageName); 
	    	}
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
